﻿'========================================================================
'功能:部門VIP設定
'作者:shihyu
'日期: 2011/5/19
'========================================================================
Imports System.Data
Partial Class WebForm_Mail_VIPDeptMaintain
    Inherits BasePage
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            hiUID.Value = ActualUID
            hiDBName.Value = dbName

            If Not ChkProgramRight(1) Then '查詢
                Me.btnQuery.Visible = False
                Me.gvVipDept.Visible = False
            End If

            If Not ChkProgramRight(2) Then '新增
                Me.btnInsert.Visible = False
                plInsert.Visible = False

            End If

            If Not ChkProgramRight(3) Then '編輯
                Me.gvVipDept.Columns(0).Visible = False

            End If

            If Not ChkProgramRight(4) Then '刪除
                Me.gvVipDept.Columns(1).Visible = False

            End If
            Me.BindDDLDept()


        End If


    End Sub
    '部門選單
    Protected Sub BindDDLDept()

        Dim Group As OAMS.BBL.GroupBBL = New OAMS.BBL.GroupBBL()
        Dim User As OAMS.DAO.UsersDAO = New OAMS.DAO.UsersDAO(dbName)
        Dim DeptID As String = User.GetUsers(hiUID.Value).Rows(0)("Dept_ID").ToString()
        '如果不是Admin則需要卡權限
        If Group.GroupMemberIsExist("Admin", hiUID.Value, hiDBName.Value) = False Then

            ddlDept.DataSource = User.GetDeptByUID(hiUID.Value)
            ddlDept.DataBind()
            ddlDept.SelectedValue = DeptID

            ddlOAFactoryIns.DataSource = User.GetDeptByUID(hiUID.Value)
            ddlOAFactoryIns.DataBind()
            ddlOAFactoryIns.SelectedValue = DeptID
            ' ddlDept.Enabled = False
        Else
            ddlDept.DataSource = User.GetDept("1")
            ddlDept.DataBind()
            ddlDept.SelectedValue = DeptID

            ddlOAFactoryIns.DataSource = User.GetDept("1")
            ddlOAFactoryIns.DataBind()
            ddlOAFactoryIns.SelectedValue = DeptID

        End If

    End Sub
    Protected Sub BindGV(ByVal Dept As String, ByVal EMP As String)
        Me.gvVipDept.Visible = True
        Dim bbl As AssetManage.BBL.VIPDeptBBL = New AssetManage.BBL.VIPDeptBBL()
        Me.gvVipDept.DataSource = bbl.GetVIPDept(Dept, EMP, dbName)
        Me.gvVipDept.DataBind()
    End Sub


    Protected Sub btnInsert_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnInsert.Click
        

    End Sub

    Protected Sub btnQuery_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnQuery.Click
        'If Me.txtEmp.Text = "" Then
        '    BasePage.ShowMessageForAjax(btnQuery, GetLocalResourceObject("EmpEmpty"))
        '    Exit Sub
        'End If
        'If Me.ddlDept.SelectedValue = "" Then
        '    BasePage.ShowMessageForAjax(btnQuery, GetLocalResourceObject("DeptEmpty"))
        '    Exit Sub
        'End If
        Me.BindGV(Me.ddlDept.SelectedValue, Me.txtEmp.Text.Trim())

    End Sub
   
    Protected Sub gvVipDept_RowDeleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles gvVipDept.RowDeleting
        Dim DeptID As String = CType(gvVipDept.Rows(e.RowIndex).FindControl("lblDept_ID"), Label).Text.Trim
        Dim bbl As AssetManage.BBL.VIPDeptBBL = New AssetManage.BBL.VIPDeptBBL()
        bbl.DeleteVIPDept(DeptID, dbName)
        Me.BindGV(Me.ddlDept.SelectedValue, Me.txtEmp.Text.Trim())
    End Sub

    Protected Sub gvVipDept_RowUpdated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdatedEventArgs) Handles gvVipDept.RowUpdated
        Me.gvVipDept.EditIndex = -1
        Me.BindGV(Me.ddlDept.SelectedValue, Me.txtEmp.Text.Trim())
    End Sub


    Protected Sub gvVipDept_RowCancelingEdit(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCancelEditEventArgs) Handles gvVipDept.RowCancelingEdit
        Me.gvVipDept.EditIndex = -1
        Me.BindGV(Me.ddlDept.SelectedValue, Me.txtEmp.Text.Trim())
    End Sub




    Protected Sub gvVipDept_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles gvVipDept.RowUpdating
        Dim EMPID As String = CType(gvVipDept.Rows(e.RowIndex).FindControl("txtEmp_ID"), TextBox).Text.Trim
        Dim DeptID As String = CType(gvVipDept.Rows(e.RowIndex).FindControl("lblDept_ID"), Label).Text.Trim

        Dim bbl As OAMS.BBL.UsersBBL = New OAMS.BBL.UsersBBL()
        If bbl.UserIsExist(EMPID, dbName) = False Then
            BasePage.ShowMessageForAjax(btnInsert, GetLocalResourceObject("EmpExistEmpty"))
            e.Cancel = True
            Exit Sub
        Else
            Dim bbl2 As AssetManage.BBL.VIPDeptBBL = New AssetManage.BBL.VIPDeptBBL()
            bbl2.UpdateVIPDept(DeptID, EMPID, UID, dbName)
            e.Cancel = True
            Me.gvVipDept.EditIndex = -1
            'Me.BindDDLDept()
            'Me.txtEmp.Text = ""
            Me.BindGV(Me.ddlDept.SelectedValue, Me.txtEmp.Text.Trim())

        End If
    End Sub

    Protected Sub gvVipDept_RowEditing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles gvVipDept.RowEditing
        gvVipDept.EditIndex = e.NewEditIndex
        me.BindGV(me.ddlDept.SelectedValue,me.txtEmp.Text.Trim)
    End Sub

  
    Protected Sub btnOpen_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnOpen.Click
        PopupFormForAjax(btnOpen, "emp_no,emp_name", GetLocalResourceObject("colUsers"), "AUOREF.dbo.emp_data_all", hiDBName.Value, "1", txtEmp.ClientID, "directories=no,location=no,menubar=no,toolbar=no,scrollbars=yes, width=500 ,left=200")

    End Sub

    Protected Sub gvVipDept_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles gvVipDept.RowCommand
        If e.CommandName = "Open" Then
            PopupFormForAjax(btnOpen, "emp_no,emp_name", GetLocalResourceObject("colUsers"), "AUOREF.dbo.emp_data_all", hiDBName.Value, "1", gvVipDept.Rows(gvVipDept.EditIndex).FindControl("txtEmp_ID").ClientID, "directories=no,location=no,menubar=no,toolbar=no,scrollbars=yes, width=500 ,left=200")

        End If
    End Sub

    Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
        If ddlOAFactoryIns.SelectedValue = "" Then
            ShowMessageForAjax(btnOpen, GetLocalResourceObject("DeptEmpty"))
            mpIns.Show()
            Return
        End If
        Dim EMPID As String = txtEmp_IDIns.Text.Trim()
        Dim DEPTID As String = ddlOAFactoryIns.SelectedValue
        Dim vip As AssetManage.BBL.VIPDeptBBL = New AssetManage.BBL.VIPDeptBBL()
        Dim user As OAMS.BBL.UsersBBL = New OAMS.BBL.UsersBBL()
        '檢查工號是否存在資料庫中
        If user.UserIsExist(EMPID, dbName) Then
            '檢查部門id是否重覆
            If vip.VIPDeptIsExist(DEPTID, dbName) = False Then

                vip.InsertVIPDept(DEPTID, EMPID, UID, dbName)
            Else
                BasePage.ShowMessageForAjax(btnInsert, GetLocalResourceObject("DeptExist"))
                mpIns.Show()

                Return
                EMPID = ""
            End If
            txtEmp.Text = ""
            Me.BindGV(DEPTID, "")
        Else
            BasePage.ShowMessageForAjax(btnInsert, GetLocalResourceObject("EmpExistEmpty"))
            mpIns.Show()
        End If
    End Sub

    Protected Sub btnOpenIns_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnOpenIns.Click
        PopupFormForAjax(btnOpen, "emp_no,emp_name", GetLocalResourceObject("colUsers"), "AUOREF.dbo.emp_data_all", hiDBName.Value, "1", txtEmp_IDIns.ClientID, "directories=no,location=no,menubar=no,toolbar=no,scrollbars=yes, width=500 ,left=200")
        mpIns.Show()
    End Sub

    Protected Sub gvVipDept_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles gvVipDept.PageIndexChanging
        gvVipDept.EditIndex = -1
        gvVipDept.PageIndex = e.NewPageIndex
        Me.BindGV(Me.ddlDept.SelectedValue, Me.txtEmp.Text.Trim())
    End Sub
End Class
